import ForceUtil from "solar/util/ForceUtil";
import {Trigger} from "solar/w3ts/handles/trigger";
import BaseUtil from "solar/util/BaseUtil";
import DataBase from "solar/common/DataBase";

/**
 开局根据存档配置 给与奖励

 根据存档配置 配置获得的条件与数量
 */
export default class MapBaseArchivesInitState {
    /**
     * key = 存档key值
     * n = 名字
     * v = 添加的值
     * t = 时间
     * id = 物编id
     */
    static config: {
        addGoldByStoredInt: { [key: string]: { v: number, n: string } },
        addLumbersByStoredInt: { [key: string]: { v: number, n: string } },
        addStoredIntOnTimer: { [key: string]: { n: string, t: number, v: number } },
        addStoredIntOnPickItem: { [id: string]: { n: string, key: string, v: number } },

    } = {
        addGoldByStoredInt: {},
        addLumbersByStoredInt: {},
        addStoredIntOnTimer: {},
        addStoredIntOnPickItem: {},
    }

    constructor() {
        BaseUtil.runLater(2, () => {
            ForceUtil.forUserForce(MapBaseArchivesInitState.forUserForce)
        })
        this.addStored()
    }


    static forUserForce(player: player): void {
        //
        let config = MapBaseArchivesInitState.config;
        for (let key in config.addGoldByStoredInt) {
            let storedInteger = DzAPI_Map_GetStoredInteger(player, key);
            if (!storedInteger || storedInteger == 0) {
                continue;
            }
            let addVars = config.addGoldByStoredInt[key]
            let addNum = addVars.v * storedInteger
            AdjustPlayerStateBJ(addNum, player, PLAYER_STATE_RESOURCE_GOLD);
            DisplayTimedTextToPlayer(player, 0, 0, 5.00, "|cffffff00您获得了存档奖励" + addVars.n + "，系统已为您增加了" + addNum + "金币！");
        }
        for (let key in config.addLumbersByStoredInt) {
            let storedInteger = DzAPI_Map_GetStoredInteger(player, key);
            if (!storedInteger || storedInteger == 0) {
                continue;
            }
            let addVars = config.addLumbersByStoredInt[key]
            let addNum = addVars.v * storedInteger
            AdjustPlayerStateBJ(addNum, player, PLAYER_STATE_RESOURCE_LUMBER);
            DisplayTimedTextToPlayer(player, 0, 0, 5.00, "|cffffff00您获得了存档奖励" + addVars.n + "，系统已为您增加了" + addNum + "木材！");
        }
    }


    addStored() {
        //
        let config = MapBaseArchivesInitState.config;
        for (let key in config.addStoredIntOnTimer) {
            let cfgData = config.addStoredIntOnTimer[key]
            let triggerHandle = CreateTrigger();
            TriggerRegisterTimerEventSingle(triggerHandle, cfgData.t);
            TriggerAddAction(triggerHandle, () => {
                ForceUtil.forUserForce(player => {
                    let newVal = MapBaseArchivesInitState.addStoreInteger(player, key, cfgData.v);
                    DisplayTimedTextToPlayer(player, 0, 0, 5.00,
                        "|cffffff00您获得了存档奖励" + cfgData.n + "，系统已为您增加了" + cfgData.v + "！当前值为:" + newVal);
                })
            });
        }
        //拾取物品时获得存档奖励
        let trigger = new Trigger();
        trigger.registerAnyUnitEvent(EVENT_PLAYER_UNIT_PICKUP_ITEM)
        trigger.addAction(() => {
            let id2str = id2string(GetItemTypeId(GetManipulatedItem()));
            let cfgData = config.addStoredIntOnPickItem[id2str]
            if (cfgData) {
                let player = GetOwningPlayer(GetTriggerUnit());
                let newVal = MapBaseArchivesInitState.addStoreInteger(player, cfgData.key, cfgData.v);
                DisplayTimedTextToPlayer(player, 0, 0, 5.00,
                    "|cffffff00您获得了存档奖励" + cfgData.n + "，系统已为您增加了" + cfgData.v + "！当前值为:" + newVal);
            }
        });


    }

    static addStoreInteger(player: player, key: string, addVal: number): number {
        let storedInteger = DataBase.getPlayerSolarData(player)[key];
        if (!storedInteger) {
            storedInteger = DzAPI_Map_GetStoredInteger(player, key);
        }
        let newVal = storedInteger + addVal;
        DataBase.getPlayerSolarData(player)[key] = newVal
        DzAPI_Map_StoreInteger(player, key, newVal);
        return newVal;
    }

}

